package io.renren.modules.common;

import io.renren.common.config.FileServerConfig;
import io.renren.common.utils.DfsUtil;
import io.renren.common.utils.FileServerResponse;
import io.renren.common.utils.R;
import org.apache.commons.lang.StringUtils;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author rdawnx
 * @package_name io.renren.modules.common
 * @class_name FileUploadController.java
 * @author_email rdawnxhi@163.com
 * @JDK_version 1.8
 * @create_on 2019/07/12 17:19
 */
@RestController
@RequestMapping("common/file/upload")
public class FileUploadController {

    @Autowired
    private FileServerConfig fileServerConfig;

    /**
     * 上传文件
     *
     * @param images
     * @return
     */
    @PostMapping("/saveFile")
    public R saveFile(@RequestParam("file") MultipartFile[] images) {
        List<UploadFile> fileList = new ArrayList<>();
        Arrays.stream(images).forEach(item -> {
            UploadFile file = new UploadFile();
            file.setName(item.getOriginalFilename());
            try {
                FileServerResponse fileServerResponse =
                        DfsUtil.uploadFileByBinary(
                                item.getOriginalFilename(),
                                item.getBytes(),
                                ContentType.create(item.getContentType()),
                                fileServerConfig.getImageUploadUrl());
//                        DfsUtil.uploadFileByMultipartFile(item, fileServerConfig.getImageUploadUrl());
                String url = fileServerResponse.getRealHttpUrl();
                // 上传后有两个http://的问题
                if (StringUtils.isNotBlank(url)) {
                    while (url.contains("http://http://")) {
                        url = url.replace("http://http://", "http://");
                    }
                }
                file.setUrl(url);
                fileList.add(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        return R.ok().put("fileList", fileList);
    }

    /**
     * 百度富文本编辑器
     *
     * @param file 上传文件
     * @return 结果
     */
    @RequestMapping(value = "/ueSaveFile")
    public Object picUpload(@RequestParam("upfile") MultipartFile file
            , HttpServletRequest request, HttpServletResponse response) {
        // 返回值
        Map<String, String> map = new HashMap<>();
        map.put("state", "SUCCESS");
        map.put("title", file.getOriginalFilename());
        map.put("original", file.getOriginalFilename());
        try {
            FileServerResponse fileServerResponse =
                    DfsUtil.uploadFileByBinary(
                            file.getOriginalFilename(),
                            file.getBytes(),
                            ContentType.create(file.getContentType()),
                            fileServerConfig.getImageUploadUrl());
//                        DfsUtil.uploadFileByMultipartFile(item, fileServerConfig.getImageUploadUrl());
            String url = fileServerResponse.getRealHttpUrl();
            // 上传后有两个http://的问题
            if (StringUtils.isNotBlank(url)) {
                while (url.contains("http://http://")) {
                    url = url.replace("http://http://", "http://");
                }
            }
            map.put("url", url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 上传文件对象
     */
    class UploadFile {
        /**
         * 原文件名
         */
        private String name;
        /**
         * 图片地址
         */
        private String url;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}
